package dao

import (
	"system_go/models"
)

func AddSpends(spends models.Spend) error {
	err := db.Table("spend").Select("specomid", "spepetid", "spenum", "speunit", "spetotal").Create(&spends).Error
	return err
}

func DelSpends(id string) error {
	err := db.Table("spend").Where("spepetid = ?", id).Delete(models.Spend{}).Error
	return err
}

func UpdateSpends(spends models.Spend) error {
	err := db.Table("spend").Where("specomid = ? and spepetid = ?", spends.Specomid, spends.Spepetid).Updates(&spends).Error
	return err
}

func GetAllSpends(spendsParam map[string]interface{}) (error, []models.Spend, int64) {
	var spendsData []models.Spend
	page := spendsParam["page"].(int)
	pageSize := spendsParam["limit"].(int)
	searchName := spendsParam["searchName"].(string)
	var total int64
	err := db.Table("spend").Where("spepetid like ? ", "%"+searchName+"%").Order("spepetid ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&spendsData).Error
	return err, spendsData, total
}
